package com.guli.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.R;
import com.guli.eduservice.bean.EduCourse;
import com.guli.eduservice.bean.EduTeacher;
import com.guli.eduservice.bean.chapter.ChapterVo;
import com.guli.eduservice.bean.vo.front.CourseQueryVo;
import com.guli.eduservice.bean.vo.front.CourseWebVo;
import com.guli.eduservice.client.OrderClient;
import com.guli.eduservice.service.EduChapterService;
import com.guli.eduservice.service.EduCourseService;
import com.guli.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EduCourseFrontController
 * @Description TODO
 * @Author YinLiangTao Email:1517535831@qq.com
 * @Date 2022/4/10 15:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/eduservice/index")
@CrossOrigin
public class EduCourseFrontController {
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduTeacherService eduTeacherService;

    @Resource
    private EduChapterService eduChapterService;

    @Resource
    private OrderClient orderClient;
    //查询前8条热门课程，查询前4个名师
    @GetMapping("index")
    public R index(){
        //查询前8条热门课程
        List<EduCourse> courseList = eduCourseService.getEightCourse();
        //查询前4个名师
        List<EduTeacher> teacherList = eduTeacherService.getFourTeacher();
        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }

    //分页显示课程列表
    @PostMapping("/getCoursePage/{page}/{limit}")
    public R getCoursePage(@PathVariable("page")long page, @PathVariable("limit")long limit,
                           @RequestBody(required = false) CourseQueryVo courseQueryVo){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        System.out.println("courseQuery**********"+courseQueryVo);
        Map<String, Object> map = eduCourseService.getCoursePage(pageParam,courseQueryVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据课程id查询详细信息")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request) {
        //根据课程id查询基础信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(courseId);

        //根据课程id查询章节小节信息
        List<ChapterVo> chapterVoList = eduChapterService.nestedList(courseId);

        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberIdByJwtToken)){
            return R.error().message("请先登录");
        }

        //查询课程是否已购买
        boolean buyCourse = orderClient.isBuyCourse(memberIdByJwtToken, courseId);
        System.out.println("***************"+buyCourse);
        return R.ok().data("course", courseWebVo).data("chapterVoList",
                chapterVoList).data("isbuy",buyCourse);

    }

    @ApiOperation(value = "根据课程id查询详细信息")
    @GetMapping(value = "getCourseWebVoById/{courseId}")
    public CourseWebVo getById2(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        //根据课程id查询基础信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(courseId);

        //根据课程id查询章节小节信息
        //List<ChapterVo> chapterVoList = eduChapterService.nestedList(courseId);
        return courseWebVo;
    }

}
